﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using LDataAccess;
using LModels.Base;
using Models.Collections;
using Proxy = LModels.Proxy.Proxy;

namespace Models
{
    public class Workspace : BaseModel
    {

       
        public DateTime Date { get; set; }
        public string Description { get; set; }
        public string Notes { get; set; }
        public string Place { get; set; }

        private RecordingCollection rec;
        public RecordingCollection Recordings
        {
            get
            {
                if (rec == null)
                    rec = new FactoryCollection<RecordingCollection>(base.px).List("ListById", new object[] { GetPK() }) as RecordingCollection;
                return rec;
            }

            set { rec = value; }
        }


        public override string ToString()
        {
            return string.Format("{0} - {1}", Date.ToShortDateString(), Description);
        }


        //public Workspace() : base() { }
        public Workspace(Proxy pxProxy) : base(pxProxy) { }

        protected internal override BaseModel Load(long IDWorkspace)
        {
            IDataReader dr = base.px.Workspace.Load(IDWorkspace);

            try
            {
                dr.Read();
                SetPK(dr["IDWorkspace"] == DBNull.Value ? -1 : (long)dr["IDWorkspace"]);

                Date = (DateTime)dr["Date"];
                Description = dr["Description"].ToString();
                Notes = dr["Notes"].ToString();
                Place = dr["Place"].ToString();

                return this;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        public void Delete()
        {

            if (GetPK().HasValue && GetPK() > -1)
                px.Workspace.Delete(GetPK().Value);

        }

        public long Save()
        {
            long? ret;
            if (GetPK().HasValue && GetPK() > -1)
            {
                update();
                ret = GetPK();
            }    
            else
            ret = save();

            if (!ret.HasValue)
            {
                throw new Exception("Unable to save!");
            }

            return ret.Value;
        }

        private long? save()
        {
            var ret = px.Workspace.Save(this);
            if (ret.HasValue)
                Load(ret.Value);

            return ret;
        }

        private void update()
        {
            px.Workspace.Update(this);
        }
    }

    namespace Collections
    {
        public class WorkspaceCollection : BaseCollection
        {
            private WorkspaceCollection(){}
            public WorkspaceCollection(Proxy pxProxy) : base(pxProxy) { }

            public WorkspaceCollection List()
            {

                WorkspaceCollection ret = new WorkspaceCollection();
                IDataReader dr = px.Workspace.List();

                while (dr.Read())
                    ret.Add(new Factory<Workspace>(px).Load((long) dr["IDWorkspace"]) as Workspace);

                return ret;

            }

        }

    }

}

